package com.polylink.sys.dao.sqlProvider;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.polylink.base.Page;

/**
 *@author xiliang.xiao
 *@date 2015年3月23日 上午9:53:20
 *
 **/
public class FunctionSqlProvider {
	private static String function_table = "cti_web_function u";

	public String list(Page p){
		BEGIN();
		SELECT("u.id id,u.name name,u.code code,u.mode_id modeId,u.remark remark");
		FROM(function_table);
		conditions(p.getQuery());
		//ORDER_BY(columns);
		return SQL()+" limit #{begin},#{pageSize}";
	}
	
	public String count(Page p){
		BEGIN();
		SELECT("count(1)");
		FROM(function_table);
		conditions(p.getQuery());
		return SQL();
	}
	
	public void conditions(Map<String,Object> query){
		String id = (String) query.get("id");
		String name = (String) query.get("name");
		String code = (String) query.get("code");
		String modeId = (String) query.get("modeId");
		String remark = (String) query.get("remark");
		
		if(StringUtils.isNotBlank(id)){
			WHERE("u.id = #{query.id}");
		}
		if(StringUtils.isNotBlank(name)){
			query.put("name", "%"+name+"%");
			WHERE("u.name like #{query.name}");
		}
		if(StringUtils.isNotBlank(code)){
			query.put("code", "%"+code+"%");
			WHERE("u.code like #{query.code}");
		}
		if(StringUtils.isNotBlank(modeId)){
			WHERE("u.mode_id = #{query.modeId}");
		}
		if(StringUtils.isNotBlank(remark)){
			query.put("remark", "%"+remark+"%");
			WHERE("u.remark like #{query.remark}");
		}
	}
}
